<?php
class ProductController extends Zend_Controller_Action
{
	public function displayAction()
	{
		// set filters an validators for GET input 
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('NotEmpty', 'Int')
        );
        
        // test if input is valid
        // retrieve requested record
        // add to view
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        if ($input->isValid()) {
        	$q = Doctrine_Query::create()
        		->from('TTF_Model_Foods f')
        		->leftJoin('f.TTF_Model_Currency r')
        		->leftJoin('f.TTF_Model_Unit u')        		        		
        		->where('f.id = ?', $input->id);
        	
        	$result = $q->fetchArray();
        	if (count($result) == 1) {
        		$this->view->food = $result[0];
        	} else {        		
        		throw new Zend_Controller_Action_Exception('Page not found' , 404);
        	}
        }        
	}
	
	public function listAction()
	{
		// set filters an validators for GET input 
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('NotEmpty', 'Int')
        );
        
        // test if input is valid
        // retrieve requested record
        // add to view
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        if ($input->isValid()) {        	        	
        	$q = Doctrine_Query::create()
        		->from('TTF_Model_Foods f')
        		->where('f.category_id = ?', $input->id);
        	$results = $q->fetchArray();
        	if (count($results) > 0) {
        		$this->view->foods = $results;
        	}        	       	
        }   
	}	
}